package com.paireach.bw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.paireach.bw.R;
import com.paireach.bw.adapter.WayFragAdapter;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.view.NoViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ${cqc} on 2017/2/3.
 */

public class WayFrag extends BaseFragment {

    private View view;
    public ViewPager viewPager2;
    private List<BaseFragment> fragmentList = new ArrayList<>();
    private TabLayout tabLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(container.getContext()).inflate(R.layout.frag_way, container, false);
        findViews();
        initViews();
        return view;
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("to_frag3")) {
            viewPager2.setCurrentItem(2);
        } else if (event.getTag().equals("to_frag2")) {
            viewPager2.setCurrentItem(1);
        }

    }

    private void initViews() {
        Frag1 frag1 = new Frag1();
        Frag2 frag2 = new Frag2();
        Frag3 frag3 = new Frag3();
        fragmentList.add(frag1);
        fragmentList.add(frag2);
        fragmentList.add(frag3);

        String[] titlesArray = getResources().getStringArray(R.array.titles);
        List<String> titlesList = Arrays.asList(titlesArray);

        WayFragAdapter adapter = new WayFragAdapter(getActivity().getSupportFragmentManager(), fragmentList, titlesList);
        viewPager2.setAdapter(adapter);
        viewPager2.setCurrentItem(0);
        viewPager2.setOffscreenPageLimit(2);

        tabLayout.setupWithViewPager(viewPager2);
    }

    private void findViews() {
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager2 = (ViewPager) view.findViewById(R.id.viewPager2);
    }


}
